package com.cxmx.escort.base;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class PagerDTO {
	// 当前第几页
	private int pageNum = 1; // 默认第1页

	// 总共多少页
	private long pageCount;

	// 每页显示几条数据
	private int pageSize = 10; // 默认10条

	// 总共多少条
	private long total;

	// 排序方式
	private String orderByParam;// 你要排序的字段名称，组成json格式，
								// 1表示升序，-1表示倒序，如：orderByParam={cretea_time:-1}

	// 必须有参数才能做分页查询
	private boolean argsCanSearch;

	// 如果为真,并且pageCount > 0 ,则在数据库中不进行查询
	private boolean userPageCount;

	// 分页返回的数据
	private Object result;

	public boolean isUserPageCount() {
		return userPageCount;
	}

	public void setUserPageCount(boolean userPageCount) {
		this.userPageCount = userPageCount;
	}

	public boolean isArgsCanSearch() {
		return argsCanSearch;
	}

	public void setArgsCanSearch(boolean argsCanSearch) {
		this.argsCanSearch = argsCanSearch;
	}

	public void init(long total) {
		this.setTotal(total);
		boolean flag = (total % this.getPageSize() == 0) ? false : true;
		long iPageSize = flag ? (total / this.getPageSize() + 1) : (total / this.getPageSize());
		// if (this.getPageNum() > iPageSize) {
		// this.setPageNum(1);
		// }
		this.setPageCount(iPageSize);
	}

	public int getPageNum() {
		return pageNum;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

	public long getPageCount() {
		return pageCount;
	}

	public void setPageCount(long pageCount) {
		this.pageCount = pageCount;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public void setPerPage(int pageSize) {
		if (pageSize > 100) {
			this.pageSize = 100;
		} else {
			this.pageSize = pageSize;
		}
	}

	public long getTotal() {
		return total;
	}

	public void setTotal(long total) {
		this.total = total;
	}

	public String getOrderByParam() {
		return orderByParam;
	}

	public void setOrderByParam(String orderByParam) {
		this.orderByParam = orderByParam;
	}

	public Object getResult() {
		return result;
	}

	public void setResult(Object result) {
		this.result = result;
	}

	public String getCacheString() {
		StringBuffer cacheKey = new StringBuffer();
		cacheKey.append(this.getPageNum()).append("#");
		cacheKey.append(this.getPageSize()).append("#");
		if (StringUtils.isNotEmpty(this.getOrderByParam())) {
			cacheKey.append(this.getOrderByParam()).append("#");
		}
		List<String> args = new ArrayList<String>();
		setCacheList(args);
		if (args != null && !args.isEmpty()) {
			for (String arg : args) {
				if (StringUtils.isNotEmpty(arg)) {
					cacheKey.append(arg).append("#");
				}
			}
		}
		return cacheKey.toString();
	}

	/**
	 * 设置缓存KEY
	 * 
	 * @param cacheKeys
	 */
	public void setCacheList(List<String> args) {

	}
}
